package main

import (
	"fmt"
	"net"
	"os"
	"os/signal"
	"runtime"
	"syscall"
	"ysnet/tcp"
)

type Callback struct {
	recvByte int
}

func (cb *Callback) OnConnect(c *tcp.Conn) error {
	return nil
}
func (cb *Callback) OnMessage(c *tcp.Conn, buff []byte) error {
	cb.recvByte += len(buff)
	return nil
}
func (cb *Callback) OnClose(c *tcp.Conn) {
	fmt.Println("recv %.2fmb", cb.recvByte/1024.0/1024.0)
}
func (cb *Callback) OnError(c *tcp.Conn, err error) {
	fmt.Printf("handle err:%v", err)
}

func test(a int) {
	if a == 0 {
		return
	}

	defer func() {
		fmt.Println("this is defer")
	}()

}

func main() {
	test(1)
	ip := net.ParseIP("error")
	fmt.Println(ip)

	runtime.GOMAXPROCS(runtime.NumCPU())

	s := tcp.NewServer(&Callback{}, &tcp.ServerConfig{
		ListenAddr:  ":8989",
		SendChanMax: 1024,
		RecvChanMax: 1024,
	})

	s.Start()

	chSig := make(chan os.Signal)
	signal.Notify(chSig, syscall.SIGINT, syscall.SIGTERM)
	fmt.Println("Signal: ", <-chSig)

	s.Close()
}
